package com.app.micaihu.base;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.app.micaihu.R;
import com.app.micaihu.listener.BackGestureListener;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.utils.HttpUtils;
import com.app.micaihu.utils.InteractiveUtils;
import com.app.utils.util.ToastUtils;
import com.app.utils.util.osutils.StatusBarUtils;
import com.baidu.mobstat.StatService;
import com.umeng.analytics.MobclickAgent;

import java.lang.reflect.Type;
import java.util.Map;

/**
 * 所有Activity的基类
 * 所有子类必须设置 mPageName 变量(便于清除网络请求)
 *
 * @author linkai 20160712
 */
@Deprecated
public class BaseOldActivity extends AppCompatActivity {

    protected String mPageName = getClass().getSimpleName();//页面标识(继承的子类一律用此变量标识页面)

    protected boolean request_state;//网络请求的请求状态

    //页面逻辑
    protected Activity mActivity;//当前activity的引用
    protected boolean pageConfigNotify;//默认不允许改变通知栏的颜色
    protected boolean isInit = true;//是否初始化页面
    protected long lastClickTime = 0;//最后点击返回的时间
    private ViewGroup mBaseContentView;//父布局

    protected int colorId;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        StatusBarUtils.setStatusBar(this);
        //获取父布局
        if (isInit) {
            mBaseContentView = (ViewGroup) getLayoutInflater().inflate(R.layout.activity_base, null);
            setContentView(mBaseContentView);
            if (colorId > 0) {
                StatusBarUtils.setupStatusBarView(this, colorId, colorId);
            } else {
                StatusBarUtils.setupStatusBarView(this, R.color.common_bg_color_2, R.color.common_bg_color_10);
                StatusBarUtils.setStatusBarTextColor(this, true);
            }
        }
        mActivity = this;
    }

    /******************
     * 以下是被继承的方法
     ***************/
    /*
     * 返回
     */
    public void doBack(View view) {
        long currentTime = System.currentTimeMillis();
        //如果连续点击的时间小于 MIN_CLICK_DELAY_TIME  则本次点击不起效
        if (currentTime - lastClickTime > 500) {
            lastClickTime = currentTime;
            onBackPressed();
        }
    }

    /**
     * 设置页面标题,以及字体颜色
     *
     * @param title
     */
    protected void setTitle(String title) {
        if (TextUtils.isEmpty(title)) {
            return;
        }
        TextView titleView = (TextView) findViewById(R.id.page_head_title);
        if (titleView != null) {
            titleView.setText(title);
            titleView.setVisibility(View.VISIBLE);
        }

    }


    /**
     * 设置title右边图标
     */
    protected ImageView setTitleRightImage(int resId) {
        ImageView pageHeadFunction = (ImageView) findViewById(R.id.page_head_function);
        if (pageHeadFunction != null) {
            pageHeadFunction.setVisibility(View.VISIBLE);
            pageHeadFunction.setImageResource(resId);
        }
        return pageHeadFunction;
    }

    /**
     * 设置title右边的文字
     */
    protected TextView setTitleRightText(String text) {
        TextView pageRightText = (TextView) findViewById(R.id.page_head_function_text);
        if (pageRightText != null) {
            pageRightText.setVisibility(View.VISIBLE);
            pageRightText.setText(text);
        }
        return pageRightText;
    }


    /**
     * title栏是否显示
     *
     * @param isShow
     */
    protected void setTitleShow(boolean isShow) {
        RelativeLayout appTitle = (RelativeLayout) findViewById(R.id.rl_apptitle);
        if (appTitle != null) {
            appTitle.setVisibility(isShow ? View.VISIBLE : View.GONE);
        }
    }

    /**
     * 设置头部title栏背景颜色
     *
     * @param resId
     */
    protected void setTitleBgColor(int resId) {
        RelativeLayout appTitle = (RelativeLayout) findViewById(R.id.rl_apptitle);
        if (appTitle != null) {
            appTitle.setBackgroundColor(getResources().getColor(resId));
        }
    }


    /**
     * 添加子布局
     *
     * @param resourceId
     */
    protected void addContentView(@LayoutRes int resourceId) {
        View view = this.getLayoutInflater().inflate(resourceId, null);
        if (mBaseContentView != null && view != null) {
            mBaseContentView.addView(view);
        } else {
            ToastUtils.showText(getString(R.string.exception_page_back));
            onBackPressed();
        }
    }

    /**
     * 添加子布局
     *
     * @param view
     */
    protected void addContentView(View view) {
        if (mBaseContentView != null && view != null) {
            mBaseContentView.addView(view);
        } else {
            ToastUtils.showText(getString(R.string.exception_page_back));
            onBackPressed();
        }
    }

    /**
     * 改变背景色
     *
     * @param resId
     */
    protected void changeBaseActivityBg(int resId) {
        if (mBaseContentView != null) {
            mBaseContentView.setBackgroundResource(resId);
        }
    }

    /**
     * 发送一个post网络请求-----DataBean.getData是可能为空的
     *
     * @param url
     * @param type
     * @param parms
     * @param httpListener
     */
    protected void addPostRequest(@NonNull String url, @NonNull Type type, Map<String, String> parms, @NonNull HttpListener<?> httpListener) {
        HttpUtils.addPostRequest(url, type, mPageName, parms, httpListener);
    }

    /**
     * 发送一个post网络请求-----DataBean.getData是可能为空的
     *
     * @param url
     * @param type
     * @param parms
     * @param httpListener
     */
    protected void addPostCacheRequest(@NonNull String url, @NonNull Type type, Map<String, String> parms, @NonNull HttpListener<?> httpListener) {
        HttpUtils.addPostCacheRequest(url, type, mPageName, parms, httpListener);
    }

    /**
     * 发送一个get网络请求
     *
     * @param url          请求地址 不为空,否则执行失败回调
     * @param type
     * @param httpListener
     */
    protected void addGetRequest(@NonNull String url, @NonNull Type type, @NonNull HttpListener<?> httpListener) {
        HttpUtils.addGetRequest(url, type, mPageName, httpListener);
    }

    /****************** 被继承的方法  end **************/

    /******************
     * 所有重写的方法  end
     **************/

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 百度统计
        StatService.onResume(this);
        // 友盟统计
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // 百度统计
        StatService.onPause(this);
        // 友盟统计
        MobclickAgent.onPause(this);
        //拉黑 点赞 关注 举报 操作存储
        InteractiveUtils.getInstance().saveLaudList();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //当页面destroy后,取消网络请求
        HttpUtils.cancelAllRequests(mPageName);
    }


    /******************私有方法  end **************/


}
